package com.dmjj.dmjjxt.controller;

import com.dmjj.common.base.BaseController;
import com.dmjj.common.base.LoginEntity;
import com.dmjj.common.result.ResultEnum;
import com.dmjj.common.result.ResultObject;
import com.dmjj.common.result.ResultPageObject;
import com.dmjj.common.util.InsertCreatManInfo;
import com.dmjj.common.util.ListCopyToVOUtil;
import com.dmjj.common.util.PaginationVOUtil;
import com.dmjj.common.vo.ResultPageVO;
import com.dmjj.common.vo.ResultVO;
import com.dmjj.dmjjxt.dto.BrandDTO;
import com.dmjj.dmjjxt.dto.TenantDTO;
import com.dmjj.dmjjxt.service.TenantService;
import com.dmjj.dmjjxt.vo.BrandVO;
import com.dmjj.dmjjxt.vo.TenantVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 商户信息表控制器
 * @Author zhangxue
 * @Date 2019-04-11 14:13:52
 */

@RestController
@Api(tags = "商户信息表控制器")
@RequestMapping("tenant")
public class TenantController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(TenantController.class);

    @Autowired
    private TenantService service;
    @Autowired
    private ResultObject resultObject;
    @Autowired
    private ResultPageObject resultPageObject;

    /**
	 * 商户信息表列表
     * @Author zhangxue
     * @Date 2019-04-11 14:13:52
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "商户信息表列表", response = TenantVO.class)
    @PostMapping("list4Page")
    public ResultPageVO<Object> list4Page(
            TenantVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize
    ){
        try {
            TenantDTO entityDTO = new TenantDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<TenantDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            List<TenantDTO> list1 = service.listByCondition(entityDTO);
            //总条数
            Integer total = list1.size();
            Integer totalPage = total/pageSize;
            if(total%pageSize!=0){
                totalPage += 1;
            }
            Boolean hasNext = false;
            if(pageNum!=null&&pageNum<totalPage){
                hasNext = true;
            }

            return resultPageObject.success(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),pageNum,totalPage,pageSize,total,hasNext,list);
        }catch (Exception e) {
            e.printStackTrace();
            return resultPageObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 商户信息表列表（无分页）
     * @Author zhangxue
     * @Date 2019-04-11 14:13:52
     * @Param
     * @Return
     */
    @ApiOperation(value = "商户信息表列表（无分页）", response = TenantVO.class)
    @PostMapping("listByCondition")
    public ResultVO<Object> listByCondition(TenantVO entity){
        try {
            TenantDTO entityDTO = new TenantDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<TenantDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, TenantVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 商户信息表详细信息
     * @Author zhangxue
     * @Date 2019-04-11 14:13:52
     * @Param
     * @Return
     */
    @ApiOperation(value = "商户信息表详细信息", response = TenantVO.class)
    @PostMapping("queryOneByPk")
    public ResultVO<Object> queryOneByPk(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            TenantDTO entityDTO = service.queryOneByPk(id);
            TenantVO entity = new TenantVO();
            BeanUtils.copyProperties(entityDTO, entity);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entity);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存商户信息表信息
     * @Author zhangxue
     * @Date 2019-04-11 14:13:52
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存商户信息表信息", response = ResultVO.class)
    @PostMapping("save")
    public ResultVO<Object> save(TenantVO entity) {
        try {
            TenantDTO t = new TenantDTO();
            BeanUtils.copyProperties(entity, t);
            LoginEntity loginEntity = getUserInfo();
            if(loginEntity==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "token不存在");
            }
            //获取创建人、创建时间,并将字段放入当前对象属性中
            t = InsertCreatManInfo.Update(t,loginEntity);
            //通过商户身份证号查询是否重复
            List<TenantDTO> list = service.queryCountByCardNo(t);
            if(list.size()>0){
                return resultObject.error(ResultEnum.FAIL.getCode(), "商户身份证号重复");
            }
            if(service.save(t) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),t);
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }


    /**
     * @Author zhangxue
     * @Description //TODO 修改商户信息表信息
     * @Date 9:08 2019/4/29
     * @Param
     * @return
     **/
    @ApiOperation(value = "修改商户信息表信息", response = ResultVO.class)
    @PostMapping("update")
    @Transactional
    public ResultVO<Object> update(TenantVO entity) {
        try {
            TenantDTO entityDTO = new TenantDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            LoginEntity loginEntity = getUserInfo();
            if(loginEntity==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "token过期");
            }
            //获取创建人、创建时间
            entityDTO = InsertCreatManInfo.UpdateUpdate(entityDTO,loginEntity);
            if(service.edit(entityDTO) == 1) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO);
            }else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除商户信息表信息
     * @Author zhangxue
     * @Date 2019-04-11 14:13:52
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除商户信息表信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "shno") String shno){
        try {
            if(service.deleteTenant(shno) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}